package com.cloudfun.campusshare.device.protocol.decoder;

import com.cloudfun.campusshare.device.protocol.response.GgyState;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.BiConsumer;

/**
 * Created by Huqin on 2020/3/12
 */
public class GgyStateMessageDecoder extends ByteToMessageDecoder<GgyState> {

    private List<FieldDecoder<GgyState>> fieldDecoderList;

    private static Map<Integer, Integer> tempLevelMap = new HashMap<>();

    static {
        tempLevelMap.put(0, 1);
        tempLevelMap.put(1, 2);
        tempLevelMap.put(3, 3);
        tempLevelMap.put(2, 4);
    }

    public GgyStateMessageDecoder() {
        fieldDecoderList = new ArrayList<>();
        fieldDecoderList.add(new DigitFieldDecoder<>(0, 1, GgyState::setCyclePhaseCD));
        fieldDecoderList.add(new DigitFieldDecoder<>(1, 1, this.tempLevelConsumer()));
        fieldDecoderList.add(new DigitFieldDecoder<>(2, 2, GgyState::setRemainingTimeMM));
    }


    private BiConsumer<GgyState, Integer> tempLevelConsumer() {
        return (state, tempLevel) -> {
            state.setTempLevel(tempLevelMap.get(tempLevel));
        };
    }

    @Override
    protected List<FieldDecoder<GgyState>> fieldDecoderList() {
        return fieldDecoderList;
    }

    @Override
    protected GgyState prepare() {
        return new GgyState();
    }

}
